//
//  JCWInviteFirendVC.swift
//  HengYouCai
//
//  Created by wll on 2018/1/8.
//  Copyright © 2018年 lwj. All rights reserved.
//  邀请好友界面--代码

import UIKit

class JCWInviteFirendVC: BaseViewController {

    var titleName:String?
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        self.loadData()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = Normal_458Color

        self.createNav()
        self.createCV()
    }
    private func createNav() {
        self.navigationItem.title =  self.titleName //"邀请好友"
        let btn = UIButton(type: .custom)
        btn.setTitle("邀请记录", for: .normal)
        btn.setTitleColor(Normal_333Color, for: .normal)
        btn.titleLabel?.font = Font_WL(15)
        btn.sizeToFit()
        btn.addTarget(self, action: #selector(rightBarClick), for: .touchUpInside)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: btn)
    }
    func loadBackImageView(url:String) {
//        bgImV.kf.setImage(with: ImageResource(downloadURL: URL(string: url)!))
        bgImV.kf_setImage(with: ImageResource(downloadURL: URL(string: url)!))
    }
    //MARK:按钮点击事件
    @objc func btnClick(sender: UIButton) {
        switch sender.tag {
        case 100://复制
            JCWPasteboard(WLFileManager.readUserData(forKey: kCusCode) as? String)
            self.view.showHUDWarnTips("复制成功", hideDelay: kHUDHideDelay)
        case 101://发送链接给好友
            let vc = JCWShareVC()//有坑 有tabBar的界面不可以
            vc.modalPresentationStyle = .overCurrentContext
            weak var weakSelf = self
            vc.shareResultHandler = { (resultStr) in
                weakSelf?.view.showHUDWarnTips(resultStr, hideDelay: kHUDHideDelay)
            }
            self.present(vc, animated: true, completion: nil)
        case 102://查看邀请奖励
            self.pushVC(BannerWebViewVC(urlStr: JCW_Yqjlgz, title:"邀请奖励规则"))
        default:
            break
        }
    }
    
    @objc func rightBarClick()  {
        self.pushVC(InviteRecordVC())
    }
    
    private func createCV() {
        self.view.addSubview(self.bgImV)
        self.bgImV.sd_layout().spaceToSuperView(UIEdgeInsetsMake(kJcwGetNavHeight(), 0, 0, 0))
        

        ///自下而上布局
        self.bgImV.addSubview(self.sepLineView)
        self.sepLineView.frame = CGRect(x: 0, y: 0, width: 0.5, height: 16.ah_wl)
        self.sepLineView.centerX_sd = self.view.centerX_sd
        self.sepLineView.bottom_sd = self.view.height_sd - kJcwGetNavHeight() - 21.ah_wl
        
        self.bgImV.addSubview(self.shareBtn)
        self.shareBtn.right_sd = self.sepLineView.left_sd - 20.aw_wl
        self.shareBtn.centerY_sd = self.sepLineView.centerY_sd
        
        self.bgImV.addSubview(self.shareRulerBtn)
        self.shareRulerBtn.left_sd = self.sepLineView.right_sd + 20.aw_wl
        self.shareRulerBtn.centerY_sd = self.sepLineView.centerY_sd
        
        self.bgImV.addSubview(self.contentImV)
        var contentImVH: CGFloat = (JCWSCREENW-60)*(self.contentImV.height_sd/self.contentImV.width_sd)
        //对粪叉特殊处理高度
        if kJcwGetNavHeight() != 64 {
            contentImVH +=  58
        }
        self.contentImV.bounds = CGRect(x: 0, y: 0, width: JCWSCREENW-60, height: contentImVH)
        self.contentImV.centerX_sd = self.view.centerX_sd
        self.contentImV.bottom_sd = self.sepLineView.top_sd - 25.ah_wl
        
        self.contentImV.addSubview(self.qrCodeImv)
        self.qrCodeImv.bounds = CGRect(x: 0, y: 0, width: 85.ah_wl, height: 85.ah_wl)
        self.qrCodeImv.centerX_sd = self.contentImV.width_sd/2
        self.qrCodeImv.centerY_sd = self.contentImV.height_sd/2
        
        self.contentImV.addSubview(self.qrCodeLab)
        self.qrCodeLab.right_sd = self.contentImV.width_sd/1.9
        self.qrCodeLab.top_sd = self.qrCodeImv.bottom_sd + 15.ah_wl
        
        self.contentImV.addSubview(self.copyBtn)
        self.copyBtn.left_sd = self.qrCodeLab.right_sd + 5.aw_wl
        self.copyBtn.centerY_sd = self.qrCodeLab.centerY_sd

        //取缓存 否则取默认
        let shareLink = WLFileManager.readUserData(forKey: kCusShareLink) as? String ?? ("https://www.ziyijinfu.com/wap/share?signcode=" + (WLFileManager.readUserData(forKey: kCusCode) as? String ?? ""))
        self.qrCodeImv.image = UIImage.wl_createQRImage(with: shareLink, bgColor: UIColor.white, contentColor: UIColor.black, size: self.qrCodeImv.size_sd)
        
        //移除 如有需要打开即可
//        self.contentImV.addSubview(self.alertLab)
//        self.alertLab.centerX_sd = self.qrCodeImv.centerX_sd
//        self.alertLab.bottom_sd = self.qrCodeImv.top_sd - 17.aw_wl
        
    }
    
    //MARK:懒加载
    ///背景视图
    lazy var bgImV: UIImageView = {
//        let temp = UIImageView(image: UIImage(named: "Invite_bg"))
        let temp = UIImageView()
        temp.isUserInteractionEnabled = true
        return temp
    }()
    
    ///框
    lazy var contentImV: UIImageView = {
        let temp = UIImageView(image: UIImage(named: "Invite_codebg"))
        temp.isUserInteractionEnabled = true
        temp.sizeToFit()
        return temp
    }()
    
    ///alertTitleLab
    lazy var alertLab: UILabel = {
        let temp = UILabel()
        temp.text = "您的专属二维码，面对面扫码邀请"
        temp.textColor = Normal_666Color
        temp.font = Font_Auto(13)
        temp.sizeToFit()
        return temp
    }()
    
    ///二维码图
    lazy var qrCodeImv: UIImageView = { 
        let temp = UIImageView()
        temp.layer.borderWidth = 0.5
        temp.layer.borderColor = UIColor.white.cgColor
        temp.sizeToFit()
        return temp
    }()
    
    ///邀请码
    lazy var qrCodeLab: UILabel = {
        let temp = UILabel()
        temp.text = WLFileManager.readUserData(forKey: kCusCode) as? String ?? " "
        temp.textColor = UIColor.white
        temp.font = Font_Auto(19)
        temp.sizeToFit()
        return temp
    }()
    
    ///复制按钮
    lazy var copyBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setTitle("复制", for: .normal)
        temp.setTitleColor(UIColor.white, for: .normal)
        temp.titleLabel?.font = Font_Auto(16)
        temp.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        temp.tag = 100
        temp.sizeToFit()
        return temp
    }()
    
    ///华丽的分割线
    lazy var sepLineView: UIView = {
        let temp = UIView()
        temp.backgroundColor = UIColor.white
        return temp
    }()
    
    ///发送链接给好友
    lazy var shareBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setTitle("发送链接给好友", for: .normal)
        temp.setTitleColor(UIColor.white, for: .normal)
        temp.titleLabel?.font = Font_Auto(16)
        temp.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        temp.tag = 101
        temp.sizeToFit()
        return temp
    }()
    ///查看奖励规则
    lazy var shareRulerBtn: UIButton = {
        let temp = UIButton(type: .custom)
        temp.setTitle("查看奖励规则", for: .normal)
        temp.setTitleColor(UIColor.white, for: .normal)
        temp.titleLabel?.font = Font_Auto(16)
        temp.addTarget(self, action: #selector(btnClick), for: .touchUpInside)
        temp.tag = 102
        temp.sizeToFit()
        return temp
    }()

}


extension JCWInviteFirendVC {
    fileprivate func loadData() {
        
        HYCNetService.homeSystem(withDict: nil, onSuccess: {  [weak self](dicResult) in
            
            let json = JSON(dicResult as Any)
            
            self?.title = json["title"].stringValue
            self?.loadBackImageView(url: json["shareBackgroundImgUrl"].stringValue)
            
            WLFileManager.saveUserData(dicResult?["shareLink"], forKey: kCusShareLink)
            WLFileManager.saveUserData(dicResult?["inviteTitle"], forKey: kCusTitle)
            WLFileManager.saveUserData(dicResult?["inviteDescription"], forKey: kCusDescrption)
            WLFileManager.saveUserData(dicResult?["shareImageUrl"], forKey: kCusShareImageUrl)
        }) { (code, errorMsg) in
            
        }
    }
}






